﻿using FluentNHibernate.Automapping;
using Messenia.Data.Hibernate.Examples.FirstAutomappedProject;
using Messenia.Data.Models;
using NHibernate;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace Messenia.Data.Hibernate {
    public class StaticHibernate {
        private static StaticHibernate  instance;
        static readonly object padlock = new object ();
        public ISessionFactory SessionFactory { get; private set; }

        private StaticHibernate () {
            new NHibernate.ByteCode.Castle.ProxyFactoryFactory ();
            SessionFactory = Fluently.Configure ()
                 .Database (MySQLConfiguration.Standard
                 .ConnectionString ("database=messenia;server=localhost;uid=root;pwd=root"))
                 .ExposeConfiguration (c => c.Properties.Add ("hbm2ddl.keywords", "none"))
                 .Mappings (m => m.AutoMappings.Add (CreateAutomappings))
                // .ExposeConfiguration (BuildSchema)
                 .BuildSessionFactory ();
        }
        static AutoPersistenceModel CreateAutomappings () {
            // This is the actual automapping - use AutoMap to start automapping,
            // then pick one of the static methods to specify what to map (in this case
            // all the classes in the assembly that contains Employee), and then either
            // use the Setup and Where methods to restrict that behaviour, or (preferably)
            // supply a configuration instance of your definition to control the automapper.
            return AutoMap.AssemblyOf<Documents> (new AutomappingConfiguration ()).UseOverridesFromAssemblyOf<Documents> ()
                .Conventions.Add<CascadeConvention> ();
        }
        /// <summary>
        // this NHibernate tool takes a configuration (with mapping info in)
        // and exports a database schema from it
        /// </summary>
        /// <param name="config"></param>
        private static void BuildSchema (Configuration config) {
            new SchemaExport (config)
                .Create (false, true);
        }
        private static void DropSchema (Configuration configuration) {
            new SchemaExport (configuration)
                .Drop (false, true);
        }

        public void ExportSchema () {
            Fluently.Configure ()
                .Database (MySQLConfiguration.Standard
                              .ConnectionString ("database=messenia;server=localhost;uid=root;pwd=root"))
                .ExposeConfiguration (c => c.Properties.Add ("hbm2ddl.keywords", "none"))
                .Mappings (m =>
                          m.AutoMappings.Add (CreateAutomappings))
                .ExposeConfiguration (BuildSchema).BuildConfiguration ();


        }

        public void DropSchema () {
            Fluently.Configure ()
                .Database (MySQLConfiguration.Standard
                              .ConnectionString ("database=messenia;server=localhost;uid=root;pwd=root"))
                .ExposeConfiguration (c => c.Properties.Add ("hbm2ddl.keywords", "none"))
                .Mappings (m =>
                          m.AutoMappings.Add (CreateAutomappings))
                .ExposeConfiguration (DropSchema).BuildConfiguration ();
        }


        public static StaticHibernate Instance {
            get {
                lock ( padlock ) {
                    if ( instance == null ) {
                        instance = new StaticHibernate ();
                    }
                }
                return instance;
            }

        }
    }




}
